package com.red.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.red.context.BaseContext;
import com.red.dto.HomePageQueryDTO;
import com.red.dto.ProductsDTO;
import com.red.entity.*;
import com.red.exception.BaseException;
import com.red.mapper.*;
import com.red.result.PageResult;
import com.red.service.ProductsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {

    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分页查询
     * @param dto
     * @return
     */
    public PageResult pageQuery(HomePageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Products> page = productsMapper.pageQuery(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 购物车购买商品
     * @param ids
     */
    public void  buyProduct(List<Long> ids) {
        Orders orders = null;
        Long user_id = BaseContext.getCurrentId();
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("user_id",user_id);
        qw.eq("is_default",1);
        //查询默认地址
        Address address = addressMapper.selectOne(qw);
        if (address==null){
            throw new BaseException("没有默认地址");
        }


        for (Long id : ids) {

            ShoppingCar shoppingCar = shoppingCarMapper.selectById(id);

            Products products = productsMapper.selectById(shoppingCar.getProductId());

            orders = new Orders();

            BeanUtils.copyProperties(address,orders);
            orders.setId(null);
            orders.setProductId(products.getId());

            orders.setCreatedAt(LocalDateTime.now());
            orders.setStatus(1);
            orders.setUserId(user_id);
            orders.setCount(shoppingCar.getNumber());
            orders.setMoney( (products.getPrice()* shoppingCar.getNumber()));
            orderMapper.insert(orders);
        }
    }

    /**
     * 购买一件商品
     * @param id
     */
    public void buyOne(Long id) {
        Long user_id = BaseContext.getCurrentId();
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("user_id",user_id);
        qw.eq("is_default",1);
        //查询默认地址
        Address address = addressMapper.selectOne(qw);

        if (address==null){
            throw new BaseException("没有默认地址");
        }
        Orders orders = new Orders();

        BeanUtils.copyProperties(address,orders);
        Products products = productsMapper.selectById(id);
        orders.setId(null);
        orders.setProductId(id);
        orders.setCreatedAt(LocalDateTime.now());
        orders.setStatus(1);
        orders.setUserId(user_id);
        orders.setMoney(products.getPrice());
        orders.setCount(1);
        orderMapper.insert(orders);
    }

    /**
     * 根据商品Id查询
     * @param id
     * @return
     */
    public Products selectById(Long id) {
        return productsMapper.selectById(id);
    }

    /**
     * 新增商品
     * @param products
     */
    @Transactional
    public void insert(Products products) {
        //TODO保存照片到oss

        productsMapper.insert(products);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteByIds(List<Long> ids) {
        productsMapper.deleteByIds(ids);
    }

    /**
     * 更新数据
     * @param dto
     */
    public void updateProducts(ProductsDTO dto) {
        //TODO判断照片是否更新

        Products products = new Products();
        BeanUtils.copyProperties(dto,products);
        productsMapper.updateById(products);

    }

    /**
     * 查询所有分类
     * @return
     */
    public List<Category> selectList() {
        return categoryMapper.selectList(new QueryWrapper<>());
    }

    /**
     * 更新分类
     * @param category
     */
    public void updateCategory(Category category) {
        categoryMapper.updateById(category);
    }

    /**
     * 删除分类
     * @param ids
     */
    public void deleteCategoryByIds(List<Long> ids) {
        QueryWrapper<Products> qw = new QueryWrapper<>();
        qw.in("category_id",ids);
        List<Products> products = productsMapper.selectList(qw);
        if (products != null && !products.isEmpty()){
            throw new BaseException("关联的有商品无法删除");
        }else{
            categoryMapper.deleteByIds(ids);
        }
    }

    /**
     * 新增分类
     * @param category
     */
    public void insrtCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        categoryMapper.insert(category);
    }

    @Override
    public List<Products> selectAll() {

        return productsMapper.selectList(new QueryWrapper<>());
    }

    /**
     * 根据分类Id查奶茶
     * @param id
     * @return
     */
    public List<Products> selectByCategoryId(Long id) {
        QueryWrapper<Products> qw = new QueryWrapper<>();
        qw.eq("category_id",id);
        qw.eq("status",1);
        return productsMapper.selectList(qw);
    }
}
